package com.quanyan.user.web;

import com.quanyan.api.APIResponse;
import com.quanyan.user.model.vo.request.ReqUserSetting;
import com.quanyan.user.model.vo.response.RespUserSetting;
import com.quanyan.user.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by jian.zhou on 2016/6/29.
 * 用户设置
 */
@RestController
@RequestMapping(value = "/api/user/setting")
public class UserSettingController {

    private static final Logger logger = LoggerFactory.getLogger(UserSettingController.class);

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 获取用户设置
     */
    @RequestMapping(value = "/my", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<RespUserSetting> myUserSetting(HttpServletRequest request){
        logger.info("star query my user setting....");
        int uid = (int)request.getAttribute("uid");
        try {
            RespUserSetting  respUserSetting = userInfoService.getUserSetting(uid);
            logger.info("end query my user setting....result is :{}",respUserSetting.toString());
            return APIResponse.returnSuccess(respUserSetting);
        } catch (Exception e) {
           logger.error("{}",e);
            return APIResponse.returnFail("系统错误");
        }

    }


    @RequestMapping(value = "/setUp", method = RequestMethod.POST)
    public APIResponse userSetting(@RequestBody ReqUserSetting reqUserSetting,HttpServletRequest request){
        logger.info("star setup my user setting....");
        int uid = (int)request.getAttribute("uid");
        try {
            userInfoService.updateUserSetting(uid,reqUserSetting);
            logger.info("end setup my user setting....");
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统错误");
        }
    }

}
